package com.example.sparkchaindemo.utils;

import android.content.Context;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtils {

    public static byte[] readStream(String filePath) throws Exception {
        FileInputStream fs = new FileInputStream(filePath);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while (-1 != (len = fs.read(buffer))) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        fs.close();
        return outStream.toByteArray();
    }


    public static void writeFile(String path, byte[] bytes) {
        boolean append = false;
        try {
            File file = new File(path);
            if (file.exists()) {
                append = true;
            } else {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(path, append);//指定写到哪个路径中
            FileChannel fileChannel = out.getChannel();
            fileChannel.write(ByteBuffer.wrap(bytes)); //将字节流写入文件中
            fileChannel.force(true);//强制刷新
            fileChannel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        return sdDir.toString();
    }

    /**
     * 递归复制 assets 目录下的 ivw 文件夹到应用私有目录
     */
    public static void copyAssetsIVWToPrivateDir(Context context, String assetDir) {
        String targetDir = context.getFilesDir().getAbsolutePath() + "/" + assetDir;
        copyAssetsDir(context, assetDir, targetDir);
    }

    private static void copyAssetsDir(Context context, String assetsDir, String targetDir) {
        try {
            String[] files = context.getAssets().list(assetsDir);
            if (files == null || files.length == 0) {
                // 是文件，直接复制
                copyAssetFile(context, assetsDir, targetDir);
            } else {
                // 是目录
                File dir = new File(targetDir);
                if (!dir.exists()) dir.mkdirs();
                for (String file : files) {
                    copyAssetsDir(context, assetsDir + "/" + file, targetDir + "/" + file);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void copyAssetFile(Context context, String assetPath, String targetPath) {
        try {
            File outFile = new File(targetPath);
            if (!outFile.exists()) {
                outFile.getParentFile().mkdirs();
                outFile.createNewFile();
                InputStream in = context.getAssets().open(assetPath);
                OutputStream out = new FileOutputStream(outFile);
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
